﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace SynergyControls
{
    /// <suMMary>
    /// Interaction logic for BPM_SliderControl.xaml
    /// </suMMary>
    [DisplayName("BPM Slider")]
    [Description("Provides tempo control")]
    [ToolboxItem(true)]
    [TemplatePart(Name = "BPM_Slider", Type = typeof(Slider))]
    public class BPM_Slider : SettingsUpdaterBase
    {
        #region Singleton Pattern

        private static BPM_Slider instance;
        public static BPM_Slider Instance
        {
            get
            {
                if (instance == null)
                    instance = new BPM_Slider();
                return (BPM_Slider)instance;
            }
        }

        #endregion Singleton Pattern

        #region Fields
        Slider slider;
        #endregion 

        #region Dependency Properties

        public static readonly DependencyProperty BPMProperty = DependencyProperty.Register("BPM", typeof(int), typeof(BPM_Slider), new PropertyMetadata(new PropertyChangedCallback(BPMChanged)));

        private static void BPMChanged(DependencyObject dependency, DependencyPropertyChangedEventArgs args)
        {
            BPM_Slider instance = dependency as BPM_Slider;
            if (instance != null)
            {
                instance.NotifyPropertyChanged("BPM");
            }

        }
        

        [Category("Common")]
        public int BPM
        {
            get
            {
                return (int)GetValue(BPMProperty);
            }
            set
            {
                SetValue(BPMProperty, value);
            }
        }

        #endregion

        #region Constructors

        static BPM_Slider()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(BPM_Slider), new FrameworkPropertyMetadata(typeof(BPM_Slider)));            
        }

        private BPM_Slider()
        { }

        #endregion

        #region Event Overrides

        public override void OnApplyTemplate()
        {
            slider = GetTemplateChild("bpmSlider") as Slider;
            slider.ValueChanged += slider_ValueChanged;
        }
        #endregion
      
        #region Event Handlers
        void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            

            if ((int)e.NewValue != (int)e.OldValue)
            {
                BPM = (int)e.NewValue;
            }
        }
        #endregion
    }
}
